package jlb.andriva.wrappers;

import jlb.andriva.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ConsumptionTotal {

    private String mTitle;
    private String mTotDolar;
    private String mTotPeso;

    public ConsumptionTotal(Element root){
        Elements tds = root.select("td");
        mTitle = tds.get(0).text().trim();
        mTotPeso = "$ " + tds.get(1).text().trim();
        mTotDolar = "u$s " + tds.get(2).text().trim();
    }

    public LinearLayout getView(Context context){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.setMargins(2, 2, 2, 2);

        LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mainParams.setMargins(2, 8, 2, 8);

        LinearLayout main = new LinearLayout(context);
        main.setLayoutParams(mainParams);
        main.setOrientation(LinearLayout.HORIZONTAL);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        LinearLayout.LayoutParams paramsRuler = new LinearLayout.LayoutParams(4, LinearLayout.LayoutParams.FILL_PARENT);

        View ruler = (View) inflater.inflate(R.layout.ruler, null);
        ruler.setLayoutParams(paramsRuler);

        main.addView(ruler);

        TextView lblTit = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTit.setGravity(Gravity.LEFT);
        lblTit.setEllipsize(TextUtils.TruncateAt.END);
        lblTit.setLayoutParams(params);
        lblTit.setTypeface(null, Typeface.BOLD);
        lblTit.setText(mTitle);

        main.addView(lblTit);

        LinearLayout datapay = new LinearLayout(context);
        datapay.setOrientation(LinearLayout.VERTICAL);
        datapay.setGravity(Gravity.RIGHT);
        datapay.setLayoutParams(params);

        TextView lblTotDolar = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTotDolar.setEllipsize(TextUtils.TruncateAt.END);
        lblTotDolar.setLayoutParams(params);
        lblTotDolar.setGravity(Gravity.RIGHT);
        lblTotDolar.setText(mTotDolar);
        if(mTotDolar.equals("u$s 0,00")){
            lblTotDolar.setTextColor(Color.GRAY);
        }
        else{
            lblTotDolar.setTypeface(null, Typeface.BOLD);
        }

        datapay.addView(lblTotDolar);

        TextView lblTotPeso = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTotPeso.setEllipsize(TextUtils.TruncateAt.END);
        lblTotPeso.setLayoutParams(params);
        lblTotPeso.setGravity(Gravity.RIGHT);
        lblTotPeso.setText(mTotPeso);
        if(mTotPeso.equals("$ 0,00")){
            lblTotPeso.setTextColor(Color.GRAY);
        }
        else{
            lblTotPeso.setTypeface(null, Typeface.BOLD);
        }

        datapay.addView(lblTotPeso);

        main.addView(datapay);

        View ruler2 = (View) inflater.inflate(R.layout.ruler, null);
        ruler2.setLayoutParams(paramsRuler);

        main.addView(ruler2);

        return main;
    }
}
